package com.eques.demo.anim

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.ListView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.eques.base.BaseActivity
import com.eques.demo.R
import com.eques.util.Config
import org.json.JSONException
import org.json.JSONObject
import java.util.concurrent.Executors


class AnimalSetCountActivity :BaseActivity() {

    private var tvMainLeftTopHint: TextView? = null
    private var tvMainTitleHint: TextView? = null
    private var rlMainHeadParent: ConstraintLayout? = null
    private var tvMainRightTopHint:TextView?=null

    private var lvData: ListView? = null
    var times = intArrayOf(1, 2, 3, 4, 5)
    var timesList = mutableListOf<Times>()
    var adapterData: CountAdapter? = null
    var chooseTimes = 0
    lateinit var bid:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_animal_set_count)
        initView()
    }



    override fun initView() {
        getIntentData()

        tvMainLeftTopHint = findViewById(R.id.tv_main_left_top_hint)
        tvMainTitleHint = findViewById(R.id.tv_main_title_hint)
        rlMainHeadParent = findViewById(R.id.rl_main_head_parent)
        tvMainRightTopHint=findViewById(R.id.tv_main_right_top_hint)
        lvData = findViewById(R.id.lv_data)


        for (key in times) {
            val timeBean: Times
            if (chooseTimes == key) {
                timeBean = Times(key, true)
            } else {
                timeBean = Times(key, false)
            }
            timesList.add(timeBean)
        }
        adapterData = CountAdapter(timesList)
        lvData?.adapter = adapterData
        Log.e("Count", timesList.toString())
        adapterData?.notifyDataSetChanged()

        lvData?.setOnItemClickListener { _, _, position, _ ->
            for (key in timesList) {
                key.choose = false
            }
            timesList[position].choose = !timesList[position].choose
            chooseTimes= timesList[position].times
            Log.e("Count", timesList.toString())
            adapterData?.notifyDataSetChanged()
        }

        initTitleView()
    }

    override fun initData() {
        TODO("Not yet implemented")
    }


    private fun getIntentData() {
        chooseTimes = intent.getIntExtra("choose_times", 1)
        bid= intent.getStringExtra(Config.BID)!!
    }

    /**
     * 初始化头部标题栏
     */
    private fun initTitleView() {
        tvMainRightTopHint?.visibility=View.VISIBLE
        tvMainRightTopHint?.text=getString(R.string.save)
        tvMainRightTopHint?.setTextColor(resources.getColor(R.color.orange))
        tvMainRightTopHint?.setOnClickListener {
            //VL2 影子设置
            val jsonObject = JSONObject()
            try {
                jsonObject.put("voice_count", chooseTimes.toString())
            } catch (e: JSONException) {
                e.printStackTrace()
            }
            Executors.newSingleThreadExecutor().submit {
                setInfo(jsonObject.toString(),bid)
            }
            finish()
        }
        tvMainLeftTopHint!!.visibility = View.VISIBLE
        tvMainLeftTopHint?.setCompoundDrawablesWithIntrinsicBounds(
            resources.getDrawable(R.mipmap.ic_invitation_menu),
            null,
            null,
            null
        )
        tvMainLeftTopHint?.setOnClickListener {
            finish()
        }
        rlMainHeadParent!!.setBackgroundColor(Color.WHITE)
        tvMainTitleHint?.visibility = View.VISIBLE
        tvMainTitleHint?.text = getString(R.string.animal_food_count)
        tvMainTitleHint?.setTextColor(resources.getColor(R.color.common_color_333333))
    }




    data class Times(val times: Int, var choose: Boolean)

    inner class CountAdapter(var data: MutableList<Times>) : BaseAdapter() {

        override fun getCount(): Int {
            return data.size
        }

        override fun getItem(p0: Int): Any {
            return data[p0]
        }

        override fun getItemId(p0: Int): Long {
            return p0.toLong()
        }

        @SuppressLint("ViewHolder", "SetTextI18n")
        override fun getView(position: Int, p1: View?, p2: ViewGroup?): View {
            var convertView = p1
            val holder: ViewHolder
            if (convertView == null) {
                holder = ViewHolder()
                convertView = LayoutInflater.from(this@AnimalSetCountActivity)
                    .inflate(R.layout.item_animal_count_layout, null)
                holder.tvTimes = convertView.findViewById(R.id.tv_times)
                holder.imgChoose = convertView.findViewById(R.id.img_choose)
                convertView.tag = holder
            } else {
                holder = convertView.tag as ViewHolder
            }

            val item = data[position]
            holder.tvTimes?.text = item.times.toString() + getString(R.string.animal_count)
            if (item.choose) {
                holder.imgChoose?.visibility=View.VISIBLE
            } else {
                holder.imgChoose?.visibility=View.GONE
            }
            return convertView!!
        }

        inner class ViewHolder {
            internal var tvTimes: TextView? = null
            internal var imgChoose: ImageView? = null
        }

    }
}